//
//  ICSysTopButton.m
//  ICome
//
//  Created by zhangrongwu on 2017/9/6.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICSysTopButton.h"
#import "ICMessageModel.h"
#import "ICMediaManager.h"
#import "ICHeaderFile.h"

@interface ICSysTopButton ()

@property (nonatomic, weak) UIImageView *imageV;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *descLabel;
@property (nonatomic, weak) UILabel *dateLabel;

@end

@implementation ICSysTopButton

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setMessageModel:(ICMessageModel *)messageModel
{
    _messageModel = messageModel;
    ICMessage *message = messageModel.message;
    NSDictionary *dic = [NSDictionary dictionaryWithJsonString:message.fileKey];
    NSString *summary = [dic objectForKey:@"summary"];
    NSString *subTitle  = [dic objectForKey:@"subTitle"];
//    NSString *filekey = [dic objectForKey:@"filekey"];
    self.imageV.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_chat_vote"];
//    [self.imageV sd_setImageWithURL:[NSURL URLWithString:[ICTools getFileMinImageURL:(filekey)]] placeholderImage:[UIImage imageNamed:@"icon_chat_vote"]];
    self.nameLabel.text = message.content;
    self.descLabel.text = subTitle;
    self.dateLabel.text = summary;

    //    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:summary];
//    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
//    [paragraphStyle1 setLineSpacing:15];
//    self.dateLabel.attributedText = paragraphStyle1;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat leftMargin = 12;
    if (_messageModel.isSender) {
        leftMargin = 12;
    } else {
        leftMargin = 15;
    }
    
    self.imageV.x = leftMargin;
    self.imageV.top =  13;
    self.imageV.width = 50;
    self.imageV.height = 50;
    
    self.nameLabel.x = self.imageV.right+10;
    self.nameLabel.y = 10;
    self.nameLabel.width = self.width - leftMargin - self.imageV.right;
    self.nameLabel.height = 15;
    
    self.descLabel.x = self.imageV.right+10;
    self.descLabel.y = self.imageV.y;
    self.descLabel.width = self.width - leftMargin - self.imageV.right;
    self.descLabel.height = 15;
    self.descLabel.centerY = self.imageV.centerY;
    
    self.dateLabel.x = self.imageV.right+10;
    self.dateLabel.y = self.descLabel.bottom+5;
    self.dateLabel.width = self.width - leftMargin*2 - self.imageV.right;
    self.dateLabel.height = 15;
}


#pragma mark - Getter

- (UIImageView *)imageV
{
    if (!_imageV) {
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.contentMode  = UIViewContentModeScaleToFill;
        [self addSubview:imageV];
        _imageV = imageV;
    }
    return _imageV;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = ICRGB(0x333333);
        [self addSubview:label];
        _nameLabel = label;
    }
    return _nameLabel;
}

- (UILabel *)descLabel
{
    if (!_descLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = ICRGB(0x999999);
//        label.numberOfLines = 0;
        [self addSubview:label];
        _descLabel = label;
    }
    return _descLabel;
}

- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = ICRGB(0x999999);
//        label.numberOfLines = 2;
        [self addSubview:label];
        _dateLabel = label;
    }
    return _dateLabel;
}

@end
